package com.example.opengl.ktexttest;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.util.AttributeSet;

import com.example.opengl.KLog;

public class Text2DView extends GLSurfaceView implements Renderer {
    private KLog kLog = new KLog("Text2DView");
    private Context kContext;

    private Text2D background = new Text2D(0, 0, 1, 1);

    public Text2DView(Context context) {
        this(context, null);
        kContext = context;
    }

    public Text2DView(Context context, AttributeSet attrs) {
        super(context, attrs);
        kLog.i("Text2DView");
        this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        this.setRenderer(this);
        this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
//        this.setZOrderOnTop(true);
        this.setZOrderMediaOverlay(true);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        kLog.i("onSurfaceCreated");
        gl.glDisable(GL10.GL_DITHER);

        gl.glClearColor(0,0,0,0);

        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glClearDepthf(1.0f);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glDepthFunc(GL10.GL_LEQUAL);

        gl.glEnable(GL10.GL_BLEND);
//        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glEnable(GL10.GL_ALPHA_TEST);

        background.loadTextureFromString(gl);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width,height);

        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();

        gl.glOrthof(0f, 1f, 0f, 1f, -1f, 1f);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

        scrollBackground1(gl);

        gl.glEnable(GL10.GL_BLEND);
//        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
        gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glEnable(GL10.GL_ALPHA_TEST);
    }

    private void scrollBackground1(GL10 gl){
        background.draw(gl);

    }
}
